package com.ylr.demo3.AnnotationAndAOP;



import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;

import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;



/**
 * @ClassName LogAnnotationAspect
 * @Description TODO
 * @Author 11
 * @Date 2021/6/22 15:14
 * @Version 1.0
 */


@Component
@Aspect
public class LogAnnotationAspect {

//    private static final Logger log = LoggerFactory.getLogger(LogTrackAspect.class);

    private static final Logger log = LoggerFactory.getLogger(LogAnnotationAspect.class);

    @Pointcut(value = "@annotation(com.ylr.demo3.AnnotationAndAOP.LogAnnotation)")
    public void logPointcut(){

    }

    @Before("logPointcut()")
    public void doBefore(){
        log.info("------LogAnnotation日志启动---"+new Date());





    }
    @Around("@annotation(LogAnnotation)")
    public Object around(ProceedingJoinPoint pjp, LogAnnotation LogAnnotation) throws Throwable {

        log.info("-aop 日志环绕阶段-" + new Date());

        ServletRequestAttributes req = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = req.getRequest();
        // 接收到请求，记录请求内容
        String url = request.getRequestURL().toString();
        String ip = IpUtil.getIpAddr(request);
        String LogValue = LogAnnotation.value();
        Object[] pipArrary = pjp.getArgs();

        if (pipArrary.length>1){ //多参,不是Map/JsonObject方式
            List<Object> argList = new ArrayList<>();
            for (Object arg : pjp.getArgs()) {
                // request/response无法使用toJSON
                if (arg instanceof HttpServletRequest) {
                    argList.add("request");
                } else if (arg instanceof HttpServletResponse) {
                    argList.add("response");
                } else {
                    argList.add(JSON.toJSON(arg));
                }
            }
            Signature signature = pjp.getSignature();
            MethodSignature methodSignature = (MethodSignature) signature;
            // 参数名数组
            String[] parameterNames = ((MethodSignature) signature).getParameterNames();
            System.out.println("参数名数组："+new ArrayList(Arrays.asList(parameterNames)));
            System.out.println("参数是："+argList.toString());
            System.out.println("logTrackValue:"+LogValue);
            System.out.println("url:"+url);
            System.out.println("ip:"+ip);
            return pjp.proceed();

        }

        Object param =  pipArrary[0];
        System.out.println("logTrackValue:"+LogValue);
        System.out.println("url:"+url);
        System.out.println("ip:"+ip);
        System.out.println("param:"+param.toString());
        return pjp.proceed();

    }

    @After("logPointcut()")
    public void doAfter(JoinPoint joinPoint){
        log.info("-aop 日志记录结束-" + new Date());
    }



}
